package com.cloudfun.campusshare.sender;

import com.alibaba.fastjson.JSON;
import com.cloudfun.campusshare.common.constant.RabbitConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/6/18 13:50
 * @description : 延时任务消息发送
 */
@Component
@Slf4j
public class DelayTaskMessageSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     *
     * @param message 消息体
     * @param delay   延迟时间
     */
    public void sendDelayMessage(DelayMessage message, Integer delay) {
        log.info("发送延时任务消息：{},延迟时间：{}", JSON.toJSONString(message), delay);
        rabbitTemplate.convertAndSend(RabbitConstants.Exchange.DELAY_EXCHANGE_NAME, RabbitConstants.routingKey.DELAY_ROUTING_KEY,
                JSON.toJSONString(message), message1 -> {
                    message1.getMessageProperties().setDelay(delay);
                    return message1;
                });
    }
}
